﻿using FastIOC.Attribute;
using FastUtil.Config;
using FastUtil.Convert;
using FastUtil.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FastCloud.Base.Config
{
    [Component]
    public class TokenUtil
    {
        private string TokenPrefix = AppConfigUtil.Configuration["Redis:TokenPrefix"];
        private string SessionTimeOut = AppConfigUtil.Configuration["Redis:SessionTimeOut"];

        public string CreateToken(string UserGuid, string LoginID, string DisplayName, string Mobile, string OUGuid, string OUName, string OUCode, string ParentOUGuid, string TenantID, string TeanantName)
        {
            string Token = TokenPrefix + "_" + Guid.NewGuid().ToString();
            RedisUtil.SetHash(Token, "LoginID", LoginID);
            RedisUtil.SetHash(Token, "UserGuid", UserGuid);
            RedisUtil.SetHash(Token, "DisplayName", DisplayName);
            RedisUtil.SetHash(Token, "Mobile", Mobile);
            RedisUtil.SetHash(Token, "OUGuid", OUGuid);
            RedisUtil.SetHash(Token, "OUGuid", OUGuid);
            RedisUtil.SetHash(Token, "OUName", OUName);
            RedisUtil.SetHash(Token, "OUCode", OUCode);
            RedisUtil.SetHash(Token, "ParentOUGuid", ParentOUGuid);
            RedisUtil.SetHash(Token, "TenantID", TenantID);
            RedisUtil.SetHash(Token, "TeanantName", TeanantName);
            RedisUtil.SetHash(Token, "SessionID", CloudContext.Current.Session.Id);
            RedisUtil.SetKeyExpire(Token, TimeSpan.FromMinutes(ValueConvert.ConvertToInt32(SessionTimeOut)));
            //同步Session
            CloudContext.Current.Session.Set("LoginID", Encoding.UTF8.GetBytes("LoginID"));
            CloudContext.Current.Session.Set("UserGuid", Encoding.UTF8.GetBytes("UserGuid"));
            CloudContext.Current.Session.Set("DisplayName", Encoding.UTF8.GetBytes("DisplayName"));
            CloudContext.Current.Session.Set("Mobile", Encoding.UTF8.GetBytes("Mobile"));
            CloudContext.Current.Session.Set("OUGuid", Encoding.UTF8.GetBytes("OUGuid"));
            CloudContext.Current.Session.Set("OUName", Encoding.UTF8.GetBytes("OUName"));
            CloudContext.Current.Session.Set("OUCode", Encoding.UTF8.GetBytes("OUCode"));
            CloudContext.Current.Session.Set("ParentOUGuid", Encoding.UTF8.GetBytes("ParentOUGuid"));
            CloudContext.Current.Session.Set("TenantID", Encoding.UTF8.GetBytes("TenantID"));
            CloudContext.Current.Session.Set("TeanantName", Encoding.UTF8.GetBytes("TeanantName"));
            return Token;
        }
    }
}
